//
//  MUPushManager.m
//  u_push_plugin
//
//  Created by yunxi on 2020/9/8.
//

#import "MUPushManager.h"
#import <UMPush/UMessage.h>
#import <UMCommon/UMCommon.h>


@implementation MUPushManager

+ (void)UMInit:(NSDictionary *_Nullable)launchOptions appkey:(NSString *)appkey channel:(NSString *)channel logEnabled:(BOOL)logEnabled pushEnabled:(BOOL)pushEnabled{
    
    //设置是否输出
    [UMConfigure setLogEnabled:logEnabled];
    //注册appkey和渠道标识
    [UMConfigure initWithAppkey:appkey channel:channel];
    
    
}

+ (void)UMInitWithMethodCall:(FlutterMethodCall *)call result:(FlutterResult)result{
    
    NSLog(@"UMInit.arguments is :%@",call.arguments);
    
    NSDictionary *arguments = call.arguments;
    NSNumber *debug = arguments[@"logEnabled"];
    NSNumber *debgeClear = arguments[@"badgeClear"];
    
    [UMConfigure setLogEnabled:[debug boolValue]];
    [UMConfigure initWithAppkey:arguments[@"appkey"] channel:arguments[@"channel"]];
    //是否开启角标清空
    [UMessage setBadgeClear:[debgeClear boolValue]];
    
}

+ (void)setLogEnabled:(BOOL)logEnabled{
    [UMConfigure setLogEnabled:logEnabled];
}

+ (void)setBadgeClear:(BOOL)debgeClear{
    [UMessage setBadgeClear:debgeClear];
}

+ (void)addTags:(NSArray<NSString *> *)tags result:(FlutterResult)result{
    
    [UMessage addTags:tags response:^(id  _Nullable responseObject, NSInteger remain, NSError * _Nullable error) {
        if (responseObject) {
            if ([[responseObject objectForKey:@"success"] isEqualToString:@"ok"]) {
                result(@{
                 @"data":@{
                         @"success":[NSNumber numberWithBool:YES],
                         @"message":@"添加成功",
                         @"remain":[NSNumber numberWithInteger:remain]
                }});
            }else
            {
                result(@{
                    @"data":@{
                            @"success":[NSNumber numberWithBool:NO],
                            @"message":[NSString stringWithFormat:@"%@",responseObject],
                            @"remain":[NSNumber numberWithInteger:remain]
                   }});
            }
        }else
        {
            result(@{
                @"data":@{
                        @"success":[NSNumber numberWithBool:NO],
                        @"message":error.localizedDescription,
                        @"remain":[NSNumber numberWithInteger:remain]
                        }
                   });
        }
    }];
}

+ (void)deleteTags:(NSArray<NSString *> *)tags result:(FlutterResult)result{
    
    [UMessage deleteTags:tags response:^(id  _Nullable responseObject, NSInteger remain, NSError * _Nullable error) {
        if (responseObject) {
            if ([[responseObject objectForKey:@"success"] isEqualToString:@"ok"]) {
                result(@{
                 @"data":@{
                         @"success":[NSNumber numberWithBool:YES],
                         @"message":@"删除成功",
                         @"remain":[NSNumber numberWithInteger:remain]
                }});
            }else
            {
                result(@{
                    @"data":@{
                            @"success":[NSNumber numberWithBool:NO],
                            @"message":[NSString stringWithFormat:@"%@",responseObject],
                            @"remain":[NSNumber numberWithInteger:remain]
                   }});
            }
        }else
        {
            result(@{
                @"data":@{
                        @"success":[NSNumber numberWithBool:NO],
                        @"message":error.localizedDescription,
                        @"remain":[NSNumber numberWithInteger:remain]
                        }
                   });
        }
    }];
    
}

+ (void)getTagsResult:(FlutterResult)result{
    
    [UMessage getTags:^(NSSet * _Nonnull responseTags, NSInteger remain, NSError * _Nullable error) {
        NSArray *tags = @[];
        
        if (responseTags.count>0) {
            tags = [responseTags allObjects];
        }

        result(@{
            @"data":@{
                @"tags":tags,
                @"message":error.localizedDescription==nil?@"":error.localizedDescription,
                @"remain":[NSNumber numberWithInteger:remain]
                }
           });
//        result([NSDictionary dictionaryWithObjectsAndKeys:[NSDictionary dictionaryWithObjectsAndKeys:tags,@"tags",error.localizedDescription,@"message",[NSNumber numberWithInteger:remain],@"remain", nil],@"data", nil]);
    }];
    
}

+ (void)addAlias:(NSString *)Alias type:(NSString *)type result:(FlutterResult)result{
    
    [UMessage addAlias:Alias type:type response:^(id  _Nullable responseObject, NSError * _Nullable error) {
        if (responseObject) {
            if ([[responseObject objectForKey:@"success"] isEqualToString:@"ok"]) {
                result(@{
                 @"data":@{
                         @"success":[NSNumber numberWithBool:YES],
                         @"message":@"添加成功",
                }});
            }else
            {
                result(@{
                    @"data":@{
                            @"success":[NSNumber numberWithBool:NO],
                            @"message":[NSString stringWithFormat:@"%@",responseObject],
                   }});
            }
        }else
        {
            result(@{
                @"data":@{
                        @"success":[NSNumber numberWithBool:NO],
                        @"message":error.localizedDescription,
                        }
                   });
        }
    }];
    
}

+ (void)removeAlias:(NSString *)Alias type:(NSString *)type result:(FlutterResult)result{
    [UMessage removeAlias:Alias type:type response:^(id  _Nullable responseObject, NSError * _Nullable error) {
        if (responseObject) {
            if ([[responseObject objectForKey:@"success"] isEqualToString:@"ok"]) {
                result(@{
                 @"data":@{
                         @"success":[NSNumber numberWithBool:YES],
                         @"message":@"删除成功",
                }});
            }else
            {
                result(@{
                    @"data":@{
                            @"success":[NSNumber numberWithBool:NO],
                            @"message":[NSString stringWithFormat:@"%@",responseObject],
                   }});
            }
        }else
        {
            result(@{
                @"data":@{
                        @"success":[NSNumber numberWithBool:NO],
                        @"message":error.localizedDescription,
                        }
                   });
        }
    }];
}

+ (void)setAlias:(NSString *)Alias type:(NSString *)type result:(FlutterResult)result{
    
    [UMessage setAlias:Alias type:type response:^(id  _Nullable responseObject, NSError * _Nullable error) {
        if (responseObject) {
            if ([[responseObject objectForKey:@"success"] isEqualToString:@"ok"]) {
                result(@{
                 @"data":@{
                         @"success":[NSNumber numberWithBool:YES],
                         @"message":@"设置成功",
                }});
            }else
            {
                result(@{
                    @"data":@{
                            @"success":[NSNumber numberWithBool:NO],
                            @"message":[NSString stringWithFormat:@"%@",responseObject],
                   }});
            }
        }else
        {
            result(@{
                @"data":@{
                        @"success":[NSNumber numberWithBool:NO],
                        @"message":error.localizedDescription,
                        }
                   });
        }
    }];
    
}


+(void)registerDeviceToken: (NSData * _Nullable) deviceToken{
    [UMessage registerDeviceToken:deviceToken];
}


@end
